// components/bottom-tab.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
          selectedIndex:Number
  },
  lifetimes:{
    attached:function() {
      let tabs = this.data.tabs;
      console.log("bottom-tab attach.")
      if(this.properties.selectedIndex<tabs.length) {
          tabs[this.properties.selectedIndex].active=true;
      }
      else {
        throw new Error("selectedIndex out of range")
      }
      this.setData({"tabs":tabs})
      
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
        tabs:[
          {
            id:"shelf",
              icon:"shelf",
              label:"书架",
              url:"/pages/shelf/shelf",
              active:false
          },
          {
            id:"store",
            icon:"shelf",
            label:"书城",
            url:'/pages/store/store',
            active:false
        },
        {
          id:"profile",
          icon:"shelf",
          label:"我的",
          url:'/pages/profile/profile',
          active:false
      }
        
        ]
  },

  /**
   * 组件的方法列表
   */
  methods: {
      goToPage(index) {
        let url = this.data.tabs[index].url;
        wx.redirectTo({
          url: url,
        })
      },
      clickTab:function(event) {
          console.log("index:"+event.currentTarget.dataset.index)
          let clickIndex = event.currentTarget.dataset.index;
          if(clickIndex==this.properties.selectedIndex) {
            return;
          }
          this.goToPage(clickIndex);

      }
  }
})
